<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/easyui_insdep_head :: easyui_insdep_head" th:with="title='出货'">
</head>
<link th:href="@{/filter/Fliter.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{/filter/Filter.js}"/>
<body>

<div class="easyui-layout" style="width:100%;height:100%;overflow: hidden">
    <div data-options="region:'west',collapsible:false,split:true" style="width:50%;overflow: hidden">
        <table id="exporttt" style="width: 100%;height:100%">
            <script type="text/javascript">
                //<![CDATA[
                $(function () {
                    $('#exporttt').treegrid({
                        idField: 'stockId',
                        treeField: 'wareName',
                        pagination: true,
                        columns: [[
                            {title: '名称', field: 'wareName', width: "200"},
                            {title: '仓库', field: 'repertoryName', width: "50", align: 'right'},
                            {
                                title: '数量', field: 'shiptItemNum', width: "50 ", align: 'right',
                                editor: {
                                    type: 'numberspinner',
                                    options: {
                                        min: 1,
                                        required: true
                                    }
                                }
                            }, {
                                field: 'shiptTgtId', hidden: true
                            },{
                                field: 'shiptTgtName',
                                title: "出货对象",
                                width: "100",
                                align: 'right',
                                editor: {
                                    type: 'combobox',
                                    options: {
                                        valueField: 'shiptTgtName',
                                        textField: 'shiptTgtName',
                                        method: 'get',
                                        hasDownArrow: true,
                                        url: '/shipmentTarget/',
                                        editable: false,
                                        onSelect: function (row) {
                                            $("#exporttt").treegrid('find', $(this).combobox('options').fatherid)['shiptTgtId'] = row.shiptTgtId;
                                        }
                                    }
                                }
                            },
                            {
                                field: 'shiptItemPx', title: '价格', width: '15%', align: 'right',
                                formatter: function (value, row, index) {
                                    if (!value) {
                                        return;
                                    }

                                    var endIndex = value.length;

                                    while (value.charAt(endIndex - 1) == 0) {
                                        endIndex--;
                                    }

                                    if (value.charAt(endIndex - 1) == '.') {
                                        endIndex--;
                                    }
                                    return "￥" + value.substring(0, endIndex);
                                },
                                editor: {
                                    type: 'numberbox', options: {
                                        precision: "8",
                                        formatter: function (value) {
                                            if (!value) {
                                                return;
                                            }

                                            var endIndex = value.length;

                                            while (value.charAt(endIndex - 1) == 0) {
                                                endIndex--;
                                            }

                                            if (value.charAt(endIndex - 1) == '.') {
                                                endIndex--;
                                            }
                                            return value.substring(0, endIndex);
                                        }
                                    }
                                }
                            }
                        ]],
                        onDblClickRow: function (row) {
                            if ($('#exporttt').treegrid('getLevel', row.stockId) == 1) {
                                return;
                            }

                            $('#exporttt').treegrid('beginEdit', row.stockId);
                        },
                        onClickRow:function (row) {
                            if(row.shiptItemPx) {
                                totalout -= parseFloat(row.shiptItemPx);
                            }

                            $('#exporttt').treegrid('endEdit', row.stockId);

                            if(row.shiptItemPx) {
                                totalout += parseFloat(row.shiptItemPx);
                            }

                            updateTotal();
                        },
                        onBeginEdit: function (row) {
                            $(this).treegrid('getEditor',{
                                id : row.stockId,
                                field : "shiptTgtName"
                            }).target.combobox('options').fatherid = row.stockId;
                        }
                    });

                    updateTotal();
                });

                var totalout = 0;
                
                function updateTotal() {
                    var pager = $('#exporttt').datagrid().datagrid('getPager');	// get the pager of datagrid
                    pager.pagination({
                        layout:[],
                        total : totalout,
                        displayMsg : "总价 {total} 元",
                        buttons: [{
                            iconCls: 'icon-remove',
                            handler: function () {
                                row = $("#exporttt").treegrid('getSelected');

                                if ($('#exporttt').treegrid('getLevel', row.stockId) == 1) {
                                    return;
                                }

                                if (row == null) {
                                    $.messager.alert('警告', '请选中需要删除的项目', 'info');
                                    return;
                                }

                                $.messager.confirm('警告', '确定删除该项目吗', function (r) {
                                    totalout -= parseFloat(row.shiptItemPx);

                                    // 先获取父节点后移除该节点
                                    var parent = $('#exporttt').treegrid('getParent', row.stockId);
                                    $('#exporttt').treegrid('remove', row.stockId);

                                    // 如果父节点没有孩子，移除父节点
                                    if ($('#exporttt').treegrid('getChildren', parent.stockId).length == 0) {
                                        $('#exporttt').treegrid('remove', parent.stockId);
                                    }

                                    updateTotal();
                                });
                            }
                        },{
                            iconCls: 'icon-save',
                            handler: function () {
                                var jsonstr="[]";
                                var jsonarray = eval('('+jsonstr+')');

                                $.each($('#exporttt').treegrid('getRoots'), function (index, item) {
                                    $.each($('#exporttt').treegrid('getChildren', item.stockId), function (index, row) {
                                        var arr  =
                                            {
                                                "wareId" : row.wareId,
                                                "repertoryId" : row.repertoryId,
                                                "shiptTgtId" : row.shiptTgtId,
                                                "shiptItemNum" : row.shiptItemNum,
                                                "shiptItemPx" : row.shiptItemPx,
                                            }
                                        jsonarray.push(arr);
                                    });
                                });

                                $.ajax({
                                    url: "/shipment/shipmentRecord",
                                    type: "POST",
                                    datType: "JSON",
                                    contentType: "application/json;charset=UTF-8",
                                    data: JSON.stringify(jsonarray),
                                    success: function (data) {
                                        var data = data == "success" ? "出货成功" : "出货失败";
                                        $.messager.alert('结果', data, 'info');
                                        $("#exporttt").treegrid('reload');
                                        $("#tt").datagrid('reload');
                                    }
                                });
                            }
                        },]
                    });
                }
                //]]>
            </script>
        </table>
    </div>
    <div data-options="region:'center',collapsible:false" style="overflow: hidden">
        <table id="tt" class="easyui-datagrid" style="width:100%;height:100%"
               toolbar="#ttb" pageList='[1,10,20,30,50]' singleSelect="true" method="get"
               pagination="true">
            <thead>
            <tr>
                <th field="wareBarCode" style="width: 15%">条形码</th>
                <th field="wareName" style="width: 40%">名称</th>
                <th field="wareDes" style="width: 15%">描述</th>
                <th field="repertoryName" style="width: 10%">仓库</th>
                <th field="stockNum" style="width: 5%">库存</th>
            </tr>
            </thead>
            <div id="ttb" style="padding:3px">
                <div id="filter"></div>
                <span>名称:</span>
                <input id="wareName" style="line-height:26px;border:1px solid #ccc"/>
                <span>描述:</span>
                <input id="wareDes" style="line-height:26px;border:1px solid #ccc"/>
                <span>条形码:</span>
                <input id="wareBarCode" style="line-height:26px;border:1px solid #ccc"/>
                <input id="componentId" type="hidden"/>
                <script type="text/javascript">
                    //<![CDATA[
                    var componentParamDetailIds = new Set();
                    //]]>
                </script>
                <input type="checkbox" id="isFuzzy" checked="checked" />模糊
                <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">查找</a>
                <a href="#" class="easyui-linkbutton" plain="true" onclick="doReSet()">重置</a>

                <script type="text/javascript">
                    //<![CDATA[
                    $(function () {
                        //单选
                        $('#filter').comboboxfilter({
                            url: '/component/filter',
                            method: 'get',
                            text: "配件",
                            onSelect: function (itemData) {
                                $("#componentId").val(itemData.id);
                                doSearch();
                                // 获取他的属性
                                $.get("/componentParameter/", {
                                    compId: itemData.id
                                }, function (data) {
                                    if (typeof (data) == typeof ("string")) {
                                        data = $.parseJSON(data);
                                    }
                                    // 遍历属性
                                    $.each(data, function (i, item) {
                                        // 新建一个div
                                        var id = "filter" + i;
                                        $('#filter').after("<div id = '" + id + "' name='Dfilter'/ >")
                                        // 为该div初始化值
                                        id = '#' + id;
                                        $(id).comboboxfilter({
                                            url: '/componentParameterDetail/filter',
                                            method: 'get',
                                            text: item.compParamName,
                                            param: {
                                                compParamId: item.compParamId
                                            },
                                            onSelect: function (itemData) {
                                                componentParamDetailIds.add(itemData.id);
                                                doSearch();
                                            },
                                            unSelect: function (itemData) {
                                                componentParamDetailIds.delete(itemData.id);
                                                doSearch();
                                            }
                                        });
                                    });
                                })
                            },
                            unSelect: function (itemData) {
                                $("#componentId").val("");
                                doSearch();
                                $("div[name='Dfilter']").each(function () {
                                    $(this).comboboxfilter('clear');
                                    $(this).remove();
                                });
                            }
                        });

                        $('#tt').datagrid({
                            url: "/stock/list",
                            onClickRow: function (index, row) {
                                if ($('#exporttt').treegrid('find', row.stockId)) {
                                    return;
                                }

                                if (!row.compName) {
                                    row.compName = "未定义";
                                }

                                // 寻找是否有分组
                                if (!$('#exporttt').treegrid('find', row.compName)) {
                                    // 没有分组,新建一个分组
                                    $("#exporttt").treegrid('append', {
                                        data: [{
                                            stockId: row.compName,
                                            wareName: row.compName,
                                        }]
                                    });
                                }

                                $("#exporttt").treegrid('append', {
                                    parent: row.compName,
                                    data: [{
                                        stockId: row.stockId,
                                        wareId: row.wareId,
                                        repertoryId: row.repertoryId,
                                        wareName: row.wareName,
                                        repertoryName: row.repertoryName,
                                        shiptItemNum : 1
                                    }]
                                });

                            }
                        });
                    });

                    function doSearch() {
                        param = {};
                        param.componentParameterDetailIds = "";

                        if ($('#wareName').val())
                            param.wareName = $('#wareName').val();
                        if ($('#wareBarCode').val())
                            param.wareBarCode = $('#wareBarCode').val();
                        if ($('#wareDes').val())
                            param.wareDes = $('#wareDes').val();
                        if ($('#componentId').val())
                            param.componentId = $('#componentId').val();
                        if (componentParamDetailIds.size > 0) {
                            componentParamDetailIds.forEach(function (item) {
                                param.componentParameterDetailIds += item + " ";
                            });
                        }
                        param.isFuzzy = $('#isFuzzy').is(':checked');

                        $('#tt').datagrid('load', param);
                    }
                    function doReSet() {
                        $('#wareName').val("");
                        $('#wareDes').val("");
                        $('#wareBarCode').val("");
                        if (!$('#isFuzzy').is(':checked')) {
                            $('#isFuzzy').click();
                        }
                        doSearch();
                    }
                    //]]>
                </script>
            </div>
        </table>
    </div>
</div>

</body>
</html>